﻿Public Class frmExclusions
    Protected Friend CnnString As String

    Private Sub frmExclusions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With DataGridView1
            Dim MyDAL As New DAL(Me.CnnString)
            Dim strSQL As String = "EXEC dbo.GenerateDataDictionaryExclusionList"

            Dim dt As DataTable = MyDAL.GetDataTable(strSQL)
            If dt Is Nothing Then
                MessageBox.Show("Unable to gather results.  Are the stored procedures installed into this database?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If

            If dt.Rows.Count > 0 Then
                .Rows.Clear()

                .AutoGenerateColumns = False
                .AutoSize = True

                Dim column As DataGridViewColumn = New DataGridViewTextBoxColumn()
                column.DataPropertyName = "Object Name"
                column.Name = "Object Name"
                column.Width = 310
                column.ReadOnly = True
                .Columns.Add(column)

                column = New DataGridViewTextBoxColumn()
                column.DataPropertyName = "Object Type"
                column.Name = "Object Type"
                column.ReadOnly = True
                .Columns.Add(column)

                column = New DataGridViewCheckBoxColumn()
                column.DataPropertyName = "Is Excluded"
                column.Name = "Is Excluded"
                .Columns.Add(column)

                For curRow As Integer = 0 To dt.Rows.Count - 1
                    .Rows.Add(dt.Rows(curRow).Item("Object Name"), _
                        dt.Rows(curRow).Item("Object Type"), _
                        dt.Rows(curRow).Item("Is Excluded") _
                    )
                Next

            End If
        End With
    End Sub

    Private Sub DataGridDemo_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim GridView As DataGridView = DirectCast(sender, DataGridView)

        If TypeOf GridView.CurrentCell Is DataGridViewCheckBoxCell Then
            AddDeleteException(GridView.CurrentCell.OwningColumn.Name)
        End If
    End Sub

    Sub AddDeleteException(ByVal FieldName As String)
        With Me.DataGridView1
            If TypeOf .CurrentCell Is DataGridViewCheckBoxCell Then
                .EndEdit() ' Must be here else you will not get the proper value   
                'Return Convert.ToBoolean(.Rows(.CurrentRow.Index).Cells(FieldName).Value)

                Dim MyDAL As New DAL(Me.CnnString)
                Dim strSQL As String = "EXEC dbo.GenerateDataDictionaryAddExclusion '" _
                    & DataGridView1.Rows(.CurrentRow.Index).Cells(0).Value.ToString & "', '" _
                    & DataGridView1.Rows(.CurrentRow.Index).Cells(1).Value.ToString & "'"


                Dim result As Object = MyDAL.RunNonQuery(strSQL)
                If result IsNot Nothing Then
                    Dim answer As DialogResult = MessageBox.Show("There was a problem updating the exclusion." & Environment.NewLine _
                        & "Do you want to see the error trace?", "Error", _
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question)

                    If answer = Windows.Forms.DialogResult.Yes Then
                        MessageBox.Show(result.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                End If
            End If
        End With
    End Sub

End Class